
package kaizone.haining.chaobohui.login;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import kaizone.haining.chaobohui.CBHApplication;
import kaizone.haining.chaobohui.json.UserCommentItem;
import kaizone.haining.chaobohui.util.BDLocationWorker;
import kaizone.haining.chaobohui.util.Utils;

import android.app.Activity;
import android.content.Context;

public class UserControl {

    public static final String REST = "http://api.dp-app.com/chao/user/remote/rest/";

    public static final String URL_REGISTER_USER = REST + "register_user.htm"; // 注册用户
    
    public static final String URL_FOLLOW_ACTIVITY = REST + "follow_activity.htm"; // 关注活动（需用户授权）

    public static final String URL_FOLLOW_SUBJECT = REST + "follow_subject.htm"; // 关注专题（需用户授权）

    public static final String URL_CHECKIN_ACTIVITY = REST + "checkin_activity.htm"; // 活动签到（需用户授权））

    public static final String URL_ADD_COMMENT = REST + "add_comment.htm"; // 添加评论
    
    public static final String URL_MY = REST + "my.htm"; // 

    public static final String URL_MY_SUBJ_FOLLOWS = REST + "my_subj_follows.htm"; // 关注专题

    public static final String URL_MY_ACT_FOLLOWS = REST + "my_act_follows.htm"; // 关注活动

    public static final String URL_MY_ACT_CHECKINS = REST + "my_act_checkins.htm"; // 活动签到
    
    public static final String URL_MY_COMMENTS = REST + "my_comments.htm"; // 我发表的评论（需用户授权）

    public static final String URL_ACTIVITY_SIGNUP = REST + "activity_signup.htm"; // 活动报名（需用户授权）

    protected String mUrl;

    protected User mUser;

    public UserControl(Context context) {
        if (context instanceof Activity) {
            CBHApplication app = (CBHApplication) ((Activity) context).getApplication();
            BDLocationWorker locationWorker = app.getBdLocationWorker();
            mUser = new User(context, locationWorker);
        } else {
            mUser = new User(context);
        }
    }

    public String requestString(String httpstring) {
        StringBuffer sb = new StringBuffer();
        sb.append(httpstring).append("?");
        sb.append("userName").append("=").append(mUser.userName).append("&");
        sb.append("userAuth").append("=").append(mUser.userAuth);
        mUrl = sb.toString();
        return mUrl;
    }

    public String requestString(String httpstring, HashMap<String, String> data) {
        if (data == null || data.isEmpty())
            return requestString(httpstring);
        StringBuilder sb = new StringBuilder();
        String ori = requestString(httpstring);
        sb.append(ori).append("&");
        Iterator iterator = data.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
            String key = entry.getKey();
            String value = entry.getValue();
            sb.append(key).append("=").append(value).append("&");
        }
        sb.replace(sb.length() - 1, sb.length(), "");
        mUrl = sb.toString();
        return mUrl;
    }
    
    /**
     * @return
     */
    public String requestMy(){
        return requestString(URL_MY);
    }

    /**
     * 关注活动（需用户授权）
     * 
     * @param id
     * @return
     */
    public String requestFollowActivity(String id) {
        HashMap<String, String> data = new HashMap<String, String>();
        String random = Utils.randomNum();
        data.put("t", random);
        data.put("activityId", id);
        return requestString(URL_FOLLOW_ACTIVITY, data);
    }

    public String requestFollowActivity(int id) {
        String idstring = String.valueOf(id);
        return requestFollowActivity(idstring);
    }

    /**
     * 关注专题（需用户授权）
     * 
     * @param subjectid
     * @return
     */
    public String requestFollowSubject(String subjectid) {
        HashMap<String, String> data = new HashMap<String, String>();
        String random = Utils.randomNum();
        data.put("t", random);
        data.put("subjectId", subjectid);
        return requestString(URL_FOLLOW_SUBJECT, data);
    }

    public String requestFollowSubject(int subjectid) {
        String subjectidstring = String.valueOf(subjectid);
        return requestFollowSubject(subjectidstring);
    }

    /**
     * 活动签到（需用户授权）
     * 
     * @param activityId
     * @param location
     * @param locationCode
     * @return
     */
    public String requestCheckinActivity(String activityId, String location, String locationCode) {
        HashMap<String, String> data = new HashMap<String, String>();
        String random = Utils.randomNum();
        data.put("t", random);
        data.put("activityId", activityId);
        data.put("location", location);
        data.put("locationCode", locationCode);
        return requestString(URL_CHECKIN_ACTIVITY, data);
    }

    public String requestCheckinActivity(String activityId) {
        HashMap<String, String> data = new HashMap<String, String>();
        String random = Utils.randomNum();
        data.put("t", random);
        data.put("activityId", activityId);
        return requestString(URL_CHECKIN_ACTIVITY, data);
    }

    public String requestCheckinActivityHasLocation(String activityId) {
    	if(!mUser.isLbs())
    		return requestCheckinActivity(activityId);
        String location = mUser.getAddrStr();
        StringBuffer locationCode = new StringBuffer();
        locationCode.append(mUser.getLongitude()).append(",").append(mUser.getLatitude());
        return requestCheckinActivity(activityId, location, locationCode.toString());
    }

    public String requestCheckinActivityHasLocation(int activityId) {
        String activityIdString = String.valueOf(activityId);
        return requestCheckinActivityHasLocation(activityIdString);
    }

    public String requestCheckinActivity(int activityId) {
        String activityIdString = String.valueOf(activityId);
        return requestCheckinActivity(activityIdString);
    }

    /**
     * 添加评论
     * 
     * @param activityId
     * @param toCommentId
     * @return
     */
    public String requestAddComment(String id, String content, String type) {
        HashMap<String, String> data = new HashMap<String, String>();
        if(type.equals(UserCommentItem.TYPE_ACT)){
        	data.put("activityId", id);        	
        }else if(type.equals(UserCommentItem.TYPE_NEWS)){
        	data.put("newsId", id);
        }
        data.put("content", content);
        return requestString(URL_ADD_COMMENT, data);
    }
    
    public String requestAddReply(String id, String toCommentId, String content, String type) {
    	HashMap<String, String> data = new HashMap<String, String>();
    	if(type.equals(UserCommentItem.TYPE_ACT)){
    		data.put("activityId", id);        	
    	}else if(type.equals(UserCommentItem.TYPE_NEWS)){
    		data.put("newsId", id);
    	}
    	 data.put("toCommentId", toCommentId);
    	data.put("content", content);
    	return requestString(URL_ADD_COMMENT, data);
    }
    
    public String requestAddReplyActivity(String id, String toCommentId, String content){
    	final String type = UserCommentItem.TYPE_ACT;
    	return requestAddReply(id, toCommentId, content, type);
    }
    
    public String requestAddReplyActivity(int id, int toCommentId,String content){
    	final String type = UserCommentItem.TYPE_ACT;
    	final String idstring = String.valueOf(id);
    	final String toCommentIdString = String.valueOf(toCommentId);
    	return requestAddReply(idstring, toCommentIdString, content, type);
    }
    
    public String requestAddReplyNews(String id, String toCommentId, String content){
    	final String type = UserCommentItem.TYPE_NEWS;
    	return requestAddReply(id, toCommentId, content, type);
    }
    
    public String requestAddReplyNews(int id, int toCommentId, String content){
    	final String type = UserCommentItem.TYPE_NEWS;
    	final String idstring = String.valueOf(id);
    	final String toCommentIdString = String.valueOf(toCommentId);
    	return requestAddReply(idstring, toCommentIdString, content, type);
    }
    
    public String requestAddCommentActivity(String id, String content){
    	final String type = UserCommentItem.TYPE_ACT;
    	return requestAddComment(id, content, type);
    }
    
    public String requestAddCommentActivity(int id, String content){
    	final String type = UserCommentItem.TYPE_ACT;
    	String idstring = String.valueOf(id);
    	return requestAddComment(idstring, content, type);
    }
    
    public String requestAddCommenNews(String id, String content){
    	final String type = UserCommentItem.TYPE_NEWS;
    	return requestAddComment(id,content, type);
    }
    
    public String requestAddCommenNews(int id, String content){
    	final String type = UserCommentItem.TYPE_NEWS;
    	final String idstring = String.valueOf(id);
    	return requestAddComment(idstring, content, type);
    }

    public String requestAddComment(int id, String content, String type) {
        String idstring = String.valueOf(id);
        return requestAddComment(idstring, content, type);
    }

    /**
     * 获取关注专题
     * 
     * @return
     */
    public String requestMySubjFollowsString() {
        return requestString(URL_MY_SUBJ_FOLLOWS);
    }

    /**
     * 获取关注活动
     * 
     * @return
     */
    public String requestMyActFollowsString() {
        return requestString(URL_MY_ACT_FOLLOWS);
    }

    /**
     * 获取活动签到
     * 
     * @return
     */
    public String requestMyActCheckinsString() {
        return requestString(URL_MY_ACT_CHECKINS);
    }
    
    /**
     * 我发表的评论（需用户授权）
     * 
     * @return
     */
    public String requestMyComments() {
    	return requestString(URL_MY_COMMENTS);
    }

    public String getUrlString() {
        return mUrl;
    }

    /**
     * 活动报名（需用户授权）
     * 
     * @param activityId
     * @param contactPhone
     * @param contactName
     * @return
     */
    public String requestActivitySignup(String activityId, String contactPhone, String contactName) {
        HashMap<String, String> data = new HashMap<String, String>();
        String random = Utils.randomNum();
        data.put("t", random);
        data.put("activityId", activityId);
        data.put("contactPhone", contactPhone);
        data.put("contactName", contactName);
        return requestString(URL_ACTIVITY_SIGNUP, data);
    }

    public String requestActivitySignup(int activityId, String contactPhone, String contactName) {
        String activityIdString = String.valueOf(activityId);
        return requestActivitySignup(activityIdString, contactPhone, contactName);
    }

}
